package v1

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"go-mall/pkg/util"
	"go-mall/service"
	"net/http"
)

func CreateCart(c *gin.Context) {
	claims, _ := util.ParseToken(c.GetHeader("Authorization"))
	var createCart service.CartService
	if err := c.ShouldBind(&createCart); err == nil {
		res := createCart.Create(c.Request.Context(), claims.ID)
		c.JSON(http.StatusOK, res)
	} else {
		fmt.Println("Error", err)
		c.JSON(http.StatusBadRequest, err)
	}
}
func ListCart(c *gin.Context) {
	claims, _ := util.ParseToken(c.GetHeader("Authorization"))
	var listCart service.CartService
	if err := c.ShouldBind(&listCart); err == nil {
		res := listCart.List(c.Request.Context(), claims.ID)
		c.JSON(http.StatusOK, res)
	} else {
		fmt.Println("Error", err)
		c.JSON(http.StatusBadRequest, err)
	}
}

func UpdateCart(c *gin.Context) {
	var updateCartService service.CartService
	if err := c.ShouldBind(&updateCartService); err == nil {
		res := updateCartService.Update(c.Request.Context(), c.Param("id"))
		c.JSON(http.StatusOK, res)
	} else {
		fmt.Println("Error", err)
		c.JSON(http.StatusBadRequest, err)
	}
}

func DeleteCart(c *gin.Context) {
	var deleteCart service.CartService
	claims, _ := util.ParseToken(c.GetHeader("Authorization"))
	res := deleteCart.Delete(c.Request.Context(), claims.ID, c.Param("id"))
	c.JSON(http.StatusOK, res)
	//if err := c.ShouldBind(&deleteCart); err == nil {
	//	res := deleteCart.Delete(c.Request.Context(), claims.ID, c.Param("id"))
	//	c.JSON(http.StatusOK, res)
	//} else {
	//	fmt.Println("Error", err)
	//	c.JSON(http.StatusBadRequest, err)
	//}
}
